package com.skycong.hello77.alarmclock;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.icu.util.Calendar;
import android.icu.util.Measure;
import android.util.AttributeSet;
import android.util.Log;
import android.util.LogPrinter;
import android.view.View;

import com.skycong.hello77.R;

import java.util.Date;

/**
 * Created by Kiki on 2018/5/30.
 */

public class AlarmClockLayout extends View {
    /**
     * 全局画布
     *
     * @param context
     */
    private Canvas mCanvas;

    /**
     * 12,3,6,9点时的画笔
     *
     * @param context
     */
    private Paint mTextPaint;

    /**
     * 测量小时文本宽度的矩形
     *
     * @param context
     */
    private Rect mTextRect = new Rect();

    /**
     * 小时圆圈画笔
     *
     * @param context
     */
    private Paint mCirclePaint;

    /**
     * 小时圆圈线条宽度
     *
     * @param context
     */
    private float mCircleStrokeWidth = 4;

    /**
     * 小时圆圈的外接矩形
     */
    private RectF mCircleRectF = new RectF();

    /**
     * 亮色，用于分针，秒针，渐变起始色
     */
    private int mLightColor;
    /**
     * 暗色，圆弧，刻度线，时针，渐变起色
     */
    private int mDarkColor;
    /**
     * 背景色
     */
    private int mBackgroundColor;
    /**
     * 小时文本字体大小
     */
    private float mTextSize;
    /**
     * 时钟半径，不包括padding值
     */
    private float mRedius;
    /**
     * 刻度线长度
     */
    private float mScaleLength;
    /**
     * 时针角度
     */
    private float mHourDegree;
    /**
     * 分针角度
     */
    private float mMinuteDegree;
    /**
     * 秒针角度
     */
    private float mSecondDegree;
    /**
     * 时针画笔
     */
    private Paint mHourHandPaint;
    /**
     * 分针画笔
     */
    private Paint mMinuteHandPaint;
    /**
     * 秒针画笔
     */
    private Paint mSecondHandPaint;
    /**
     * 时针路劲
     */
    private Path mHourHandPath = new Path();
    /**
     * 分针路劲
     */
    private Path mMinuteHandPath = new Path();
    /**
     * 秒针路劲
     */
    private Path mSecondHandPath = new Path();
    /**
     * 加一个默认padding,防止用camera旋转时钟造成四周超出view大小
     */
    private float mDefaultPadding;
    private float mPaddingLeft;
    private float mPaddingTop;
    private float mPaddingRight;
    private float mPaddingBottom;
    /**
     * 梯度扫描渐变
     */
    private SweepGradient mSweepGradient;
    /**
     * 渐变矩阵，作用在SweepGradient
     */
    private Matrix mGradientMatrix;
    /**
     * 指针在x轴的位移
     */
    private float mCanvasTranslateX;
    /**
     * 指针在y轴的位移
     */
    private float mCanvasTranslateY;
    /**
     * 指针最大的位移
     */
    private float mMaxCanvasTranslate;
    /**
     * 刻度圆弧画笔
     */
    private Paint mScaleArcPaint;
    /**
     * 刻度圆弧外接矩形
     */
    private RectF mScaleArcRectF = new RectF();
    /**
     * 刻度画笔
     */
    private Paint mScaleLinePaint;

    public AlarmClockLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.AlarmClockLayout, 0, 0);
        mBackgroundColor = ta.getColor(R.styleable.AlarmClockLayout_clock_backgroundColor, Color.parseColor("#237EAD"));
        mLightColor = ta.getColor(R.styleable.AlarmClockLayout_clock_lightColor, Color.parseColor("#ffffff"));
        mDarkColor = ta.getColor(R.styleable.AlarmClockLayout_clock_darkColor, Color.parseColor("#80ffffff"));
        mTextSize = ta.getDimension(R.styleable.AlarmClockLayout_clock_textSize, DensityUtils.sp2px(context, 14));
        ta.recycle();
        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//消除混叠现象，消除走样，图形保真
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setColor(mDarkColor);
        //居中绘制文字
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setTextSize(mTextSize);
        /**
         * 小时圆圈画笔
         */
        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setStyle(Paint.Style.STROKE);
        mCirclePaint.setStrokeWidth(mCircleStrokeWidth);
        mCirclePaint.setColor(mDarkColor);
        /**
         * 刻度画笔
         */
        mScaleLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mScaleLinePaint.setStyle(Paint.Style.STROKE);
        mScaleLinePaint.setColor(mBackgroundColor);
        /**
         * 刻度圆弧画笔
         */
        mScaleArcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mScaleArcPaint.setStyle(Paint.Style.STROKE);
        /**
         * 渐变矩阵，作用在SweepGradient
         */
        mGradientMatrix = new Matrix();
        /**
         * 秒针画笔
         */
        mSecondHandPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mSecondHandPaint.setStyle(Paint.Style.FILL);
        mSecondHandPaint.setColor(mLightColor);
        /**
         * 时针画笔
         */
        mHourHandPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mHourHandPaint.setStyle(Paint.Style.FILL);
        mHourHandPaint.setColor(mDarkColor);
        /**
         * 分针画笔
         */
        mMinuteHandPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mMinuteHandPaint.setStyle(Paint.Style.FILL);
        mMinuteHandPaint.setColor(mLightColor);


    }

    /**
     * onMeasure方法作用时测量空间的大小
     *在View放置到父容器时调用
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureDimension(widthMeasureSpec), measureDimension(heightMeasureSpec));

    }

    private int measureDimension(int measureSpec) {
        int defaultSize = 800;
        //View的尺寸
        int model = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        switch (model) {
            case MeasureSpec.EXACTLY:
                return size;
            case MeasureSpec.AT_MOST:
                return Math.min(size, defaultSize);
            case MeasureSpec.UNSPECIFIED:
                return defaultSize;
            default:
                return defaultSize;
        }

    }

    /**
     *  在控件大小发生改变时调用。所以这里初始化会被调用一次
     *  获取控件的宽和高度
     * @param w
     * @param h
     * @param oldw
     * @param oldh
     */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRedius = Math.min(w-getPaddingLeft()+w/2-mRedius+getPaddingLeft(),h-getPaddingTop()-getPaddingBottom())/2;
        mDefaultPadding = 0.12f * mRedius;
        mPaddingLeft = mDefaultPadding+w/2-mRedius+getPaddingLeft();
        mPaddingRight = mDefaultPadding+w/2-mRedius+getPaddingRight();
        mPaddingTop = mDefaultPadding+h/2-mRedius+getPaddingTop();
        mPaddingBottom = mDefaultPadding+h/2-mRedius+getPaddingBottom();
        /**
         * 刻度线长度
         */
        mScaleLength = 0.012f * mRedius;//根据比例确定刻度线长度
        /**
         * 刻度画笔
         */
        mScaleLinePaint.setStrokeWidth(0.012f * mRedius);
        /**
         * 刻度圆弧画笔
         */
        mScaleArcPaint.setStrokeWidth(mScaleLength);
        /**
         * 梯度扫描渐变，以（w/2,h/2）为中心点，两种起止颜色梯度渐变
         * float数组表示，【0,0.75】为起止颜色所占比例
         */
        mSweepGradient = new SweepGradient(w/2,h/2,new int[]{mDarkColor,mLightColor},new float[]{0.75f,1});

    }
    /**
     * 化小闹钟
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        mCanvas = canvas;
        getCurrentTime();
        drawOutSiseArc();
        drawScaleLine();
        drawSecondNeedle();
        drawMinuteNeedle();
        drawHourHand();
        invalidate();
    }

    private void drawHourHand() {
    }

    private void drawMinuteNeedle() {
    }

    private void drawSecondNeedle() {
    }

    private void drawScaleLine() {
    }

    private void drawOutSiseArc() {
    }

    /**
     * 获取当前时间
     * @return
     */
    public void getCurrentTime() {
       Long current = System.currentTimeMillis();
        Log.i("msg","毫秒数"+current);
    }
}
